using System;
using FireStarter.Base.BusinessObjectMerger;
using Firestarter.DB.Schema.BO;
using Firestarter.DB.Schema.Test.Logic;
using Habanero.Base;
using Habanero.Testability;
using NUnit.Framework;

namespace Firestarter.Test.Base.BusinessObjectMerger
{
    [TestFixture]
    public class TestBoComparer : TestBase
    {
        [SetUp]
        public override void SetupTest()
        {
            //Runs every time that any testmethod is executed
            base.SetupTest();
        }

        [TestFixtureSetUp]
        public override void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            base.TestFixtureSetup();
        }


        [Test]
        public void Test_DifferentObjectTypes()
        {
            //---------------Set up test pack-------------------
            BoComparer boComparer = new BoComparer();
            DBTable dbTable = new DBTable();
            DBDatabase dbDatabase = new DBDatabase();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            InvalidCastException exception = null;
            try
            {
                boComparer.Equals(dbTable, dbDatabase);
            }
            catch (InvalidCastException ex)
            {
                exception = ex;
            }
            //---------------Test Result -----------------------
            Assert.IsNotNull(exception, "Exception of type InvalidCastException expected.");
            Assert.AreEqual
                ("Cannot cast object of type DBTable to the type DBDatabase for comparison.", exception.Message);
        }

        [Test]
        public void Test_Equals()
        {
            //---------------Set up test pack-------------------
            BoComparer boComparer = new BoComparer();
            DBTable dbTable = new DBTable();
            dbTable.DatabaseID = Guid.NewGuid();
            dbTable.TableName = GetRandomString();
            DBTable dbTableInfo2 = new DBTable();
            dbTableInfo2.DatabaseID = Guid.NewGuid();
            dbTableInfo2.TableName = dbTable.TableName;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            bool areEqual = boComparer.Equals(dbTable, dbTableInfo2);
            //---------------Test Result -----------------------
            Assert.IsTrue(areEqual);
        }

        private static string GetRandomString()
        {
            return RandomValueGen.GetRandomString();
        }

        [Test]
        public void Test_Equals_BothNull()
        {
            //---------------Set up test pack-------------------
            BoComparer boComparer = new BoComparer();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            bool areEqual = boComparer.Equals(null, null);
            //---------------Test Result -----------------------
            Assert.IsTrue(areEqual);
        }

        [Test]
        public void Test_NotEquals_OnlyFirstIsNull()
        {
            //---------------Set up test pack-------------------
            BoComparer boComparer = new BoComparer();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            bool areEqual = boComparer.Equals(null, new DBTable());
            //---------------Test Result -----------------------
            Assert.IsFalse(areEqual);
        }

        [Test]
        public void Test_NotEquals_OnlySecondIsNull()
        {
            //---------------Set up test pack-------------------
            BoComparer boComparer = new BoComparer();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            bool areEqual = boComparer.Equals(new DBTable(), null);
            //---------------Test Result -----------------------
            Assert.IsFalse(areEqual);
        }

        [Test]
        public void Test_Equals_False()
        {
            //---------------Set up test pack-------------------
            BoComparer boComparer = new BoComparer();
            DBTable dbTable = new DBTable();
            dbTable.DatabaseID = Guid.NewGuid();
            dbTable.TableName = GetRandomString();
            dbTable.TableComment = GetRandomString();


            DBTable dbTableInfo2 = new DBTable();
            dbTableInfo2.DatabaseID = Guid.NewGuid();
            dbTableInfo2.TableName = GetRandomString();
            dbTableInfo2.TableComment = GetRandomString();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            bool areEqual = boComparer.Equals(dbTable, dbTableInfo2);
            //---------------Test Result -----------------------
            Assert.IsFalse(areEqual);
        }

        [Test]
        public void Test_Equals_True_WithIgnoreProperty()
        {
            //---------------Set up test pack-------------------
            BoComparer boComparer = new BoComparer
                (delegate(IBOProp prop) { return prop.PropertyType == typeof (Guid) || prop.PropertyName == "TableNameDB"; });
            DBTable dbTable = new DBTable();
            dbTable.DatabaseID = Guid.NewGuid();
            dbTable.TableName = GetRandomString();
            dbTable.TableComment = GetRandomString();

            DBTable dbTableInfo2 = new DBTable();
            dbTableInfo2.DatabaseID = Guid.NewGuid();
            dbTableInfo2.TableName = GetRandomString();
            dbTableInfo2.TableComment = dbTable.TableComment;

            //---------------Assert Precondition----------------
            Assert.AreEqual(dbTable.TableComment, dbTableInfo2.TableComment);
            Assert.AreNotEqual(dbTable.TableName, dbTableInfo2.TableName);
            //---------------Execute Test ----------------------
            bool areEqual = boComparer.Equals(dbTable, dbTableInfo2);
            //---------------Test Result -----------------------
            Assert.IsTrue(areEqual);
        }
    }
}